from datetime import datetime
from app import db


class WithdrawRecord(db.Model):
    """提现记录模型"""
    __tablename__ = 'withdraw_records'

    id = db.Column(db.Integer, primary_key=True)
    matchmaker_id = db.Column(db.Integer, db.ForeignKey('matchmakers.id', ondelete='CASCADE'), nullable=False, comment='红娘ID')
    amount = db.Column(db.Numeric(10, 2), nullable=False, comment='提现金额')
    fee = db.Column(db.Numeric(10, 2), default=0.00, comment='手续费')
    actual_amount = db.Column(db.Numeric(10, 2), nullable=False, comment='实际到账金额')
    withdraw_method = db.Column(db.String(20), nullable=False, comment='提现方式：wechat微信,alipay支付宝,bank银行卡')
    account_info = db.Column(db.String(200), comment='提现账户信息')
    status = db.Column(db.Integer, default=1, comment='状态：1申请中2处理中3成功4失败5已取消')
    apply_time = db.Column(db.DateTime, default=datetime.utcnow, comment='申请时间')
    process_time = db.Column(db.DateTime, comment='处理时间')
    complete_time = db.Column(db.DateTime, comment='完成时间')
    admin_note = db.Column(db.Text, comment='管理员备注')
    transaction_no = db.Column(db.String(100), comment='交易单号')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')

    # 索引
    __table_args__ = (
        db.Index('idx_matchmaker_id', 'matchmaker_id'),
        db.Index('idx_status', 'status'),
        db.Index('idx_apply_time', 'apply_time'),
    )

    def __repr__(self):
        return f'<WithdrawRecord {self.id}>'

    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'matchmaker_id': self.matchmaker_id,
            'amount': float(self.amount) if self.amount else 0,
            'fee': float(self.fee) if self.fee else 0,
            'actual_amount': float(self.actual_amount) if self.actual_amount else 0,
            'withdraw_method': self.withdraw_method,
            'withdraw_method_text': self._get_withdraw_method_text(),
            'account_info': self.account_info,
            'status': self.status,
            'status_text': self._get_status_text(),
            'apply_time': self.apply_time.strftime('%Y-%m-%d %H:%M:%S') if self.apply_time else None,
            'process_time': self.process_time.strftime('%Y-%m-%d %H:%M:%S') if self.process_time else None,
            'complete_time': self.complete_time.strftime('%Y-%m-%d %H:%M:%S') if self.complete_time else None,
            'admin_note': self.admin_note,
            'transaction_no': self.transaction_no,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        }
    
    def _get_withdraw_method_text(self):
        """获取提现方式文本"""
        method_map = {
            'wechat': '微信',
            'alipay': '支付宝',
            'bank': '银行卡'
        }
        return method_map.get(self.withdraw_method, '未知')
    
    def _get_status_text(self):
        """获取状态文本"""
        status_map = {
            1: '申请中',
            2: '处理中',
            3: '成功',
            4: '失败',
            5: '已取消'
        }
        return status_map.get(self.status, '未知') 